package com.yan.test.work.testpaging3.net;

import android.content.Context;
import android.text.TextUtils;
import android.webkit.URLUtil;


import java.io.File;
import java.util.List;
import java.util.Map;

import okhttp3.Callback;
import okhttp3.Response;


/**
 * 基础API
 * @Author Jian
 * @Time 2016/11/28
 */

public class BaseAPI {




    /**
     * get请求
     *
     * @param context
     * @param urlOrPath       完整url或路径
     * @param params          请求参数
     * @param callback
     */
    protected static void get(Context context, String urlOrPath, RequestParams params, Callback callback) {
        L.e("url" , urlOrPath);
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).get(context, urlOrPath, params, callback);
    }

    /**
     * post请求
     *
     * @param context
     * @param urlOrPath       完整url或路径
     * @param params          请求参数
     * @param callback
     */
    protected static void post(Context context, String urlOrPath, RequestParams params, Callback callback){
        post( context, true ,urlOrPath,  params,  callback);
    }

    protected static void post(Context context, boolean isChild ,String urlOrPath, RequestParams params, Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).post(context, isChild ,urlOrPath, params, callback);
    }

    protected static Response asyncPost(Context context ,String urlOrPath, RequestParams params) {
        return asyncPost( context,  true , urlOrPath,  params);
    }

    protected static Response asyncPost(Context context, boolean isChild ,String urlOrPath, RequestParams params) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        return HttpUtil.getInstance(context).asyncPost(context, isChild ,urlOrPath, params);
    }


    protected static void postWithFile(Context context, String urlOrPath, RequestParams params , Map<String , File> fileMap , Callback callback) {
        postWithFile( context,   true , urlOrPath,  params ,fileMap ,  callback);
    }
    protected static void postWithFile(Context context,  boolean isChild,String urlOrPath, RequestParams params , Map<String , File> fileMap , Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).formUpload(context, isChild ,urlOrPath ,params , fileMap , callback);
    }

    /**
     * post json
     *
     * @param context
     * @param urlOrPath       完整url或路径
     * @param entity          请求参数  StringEntity
     * @param callback
     */
    protected static void postJson(Context context, String urlOrPath, String entity, Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).postJson(context, urlOrPath, entity, callback);
    }

    /**
     * post上传请求
     *
     * @param context
     * @param urlOrPath       完整url或路径
     * @param paraMap          请求参数
     * @param fileMap          请求文件
     * @param callback
     */
    protected static void postFile(Context context, String urlOrPath, Map<String ,String > paraMap, Map<String , List<String>> fileMap, Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).formUpload(context, urlOrPath, paraMap, fileMap,  callback);
    }

    protected static void postFile(Context context, String urlOrPath, File file, Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).formUpload(context, urlOrPath ,file , callback);
    }

    /**
     * put 请求
     *
     * @param context
     * @param urlOrPath
     * @param params
     * @param callback
     */
    protected static void put(Context context, String urlOrPath, RequestParams params, Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).put(context, urlOrPath, params, callback);
    }

    /**
     * put json
     *
     * @param context
     * @param urlOrPath       完整url或路径
     * @param entity          请求参数  StringEntity
     * @param callback
     */
    protected static void putJson(Context context, String urlOrPath, String entity, Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).putJson(context, urlOrPath, entity, callback);
    }
    /**
     * delete json
     *
     * @param context
     * @param urlOrPath       完整url或路径
     * @param entity          请求参数  StringEntity
     * @param callback
     */
    protected static void deleteJson(Context context, String urlOrPath, String entity, Callback callback) {
        if (!URLUtil.isHttpUrl(urlOrPath))
            urlOrPath = HostManager.hostUrl + urlOrPath;
        HttpUtil.getInstance(context).deleteJson(context, urlOrPath, entity, callback);
    }
}
